package com.ruoyi.project.newhope.transfer.util;

import com.alibaba.fastjson.JSON;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.newhope.transfer.domain.TransferOracleEBSModel;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Slf4j
@Component
@ConfigurationProperties(prefix = "oracle")
public class OracleEBSUtil {

	private static String organizationId;
	private static String ganGuanCode;
	private static String ifaceCode;
	private static String address;

	// 传输 oracle EBS
	public static AjaxResult transferDataToEBS(List<TransferOracleEBSModel> list) throws Exception{
		log.info("----->update oracle ebs source: {}", JSON.toJSONString(list));
		CommonInvoke webserviceBody = new CommonInvoke();
		String ESB_SN = webserviceBody.createSN(getIfaceCode());
		int lineCount = 3;//传输质检项目个数。近红外系统根据系统设置检测项目计算此项目质量
		List<String> transmitQCData = new ArrayList();
		String testQCData = getTransferData(list);
		log.info("----->update oracle ebs QCDATA: {}", testQCData);
		transmitQCData.add(testQCData);                    
		String requestData = webserviceBody.createRequestData(transmitQCData, getIfaceCode(), ESB_SN,lineCount);//接口打包数据
		log.info("----->update oracle ebs RequstData: {}", Base64Util.decodeStr(requestData));
		transmitQCData.clear();
		String outputData = "";// 返回是否成功消息
		outputData = webserviceBody.invokeWS(getIfaceCode(), requestData, ESB_SN);
		log.info("update oracle ebs response: {}", outputData);
		Document documentOutput = DocumentHelper.parseText(outputData);
		Element root = documentOutput.getRootElement();
		HashMap<String, String> map = new HashMap<>();
		map = webserviceBody.getNodes(root, map);
		String returnCode = map.get("RETURN_CODE");
		if (Objects.equals(returnCode, "S000A000")) {
			return AjaxResult.success();
		}
		String returnDesc = map.get("RETURN_DESC");
		return AjaxResult.error(String.format("上传 oracleEbs 失败 错误码=%s; 错误描述=%s", returnCode, returnDesc));
	}

	public static String getTransferData(List<TransferOracleEBSModel> list) {
		TransferOracleEBSModel model = list.get(0);
		StringBuilder sbStr = new StringBuilder();
		for (TransferOracleEBSModel oracleEBSModel : list) {
			sbStr.append("<inv:P_REQUEST_LINE_ITEM>"
					+ "<VALUE1>"+getOrganizationId()+"</VALUE1>"
					+ "<VALUE2>"+oracleEBSModel.getMaterialCode()+"</VALUE2>"//物料编码
					+ "<VALUE3>"+oracleEBSModel.getBatchNum()+"</VALUE3>"//批次号
					+ "<VALUE4>"+oracleEBSModel.getInspectionCode()+"</VALUE4>"//检测项目编码
					+ "<VALUE5>"+oracleEBSModel.getInspectionValue()+"</VALUE5>"//检测值
					+ "</inv:P_REQUEST_LINE_ITEM>");
		}
		//添加默认值感官
		sbStr.append("<inv:P_REQUEST_LINE_ITEM>"
				+ "<VALUE1>"+getOrganizationId()+"</VALUE1>"
				+ "<VALUE2>"+model.getMaterialCode()+"</VALUE2>"//物料编码
				+ "<VALUE3>"+model.getBatchNum()+"</VALUE3>"//批次号
				+ "<VALUE4>"+getGanGuanCode()+"</VALUE4>"//检测项目编码
				+ "<VALUE5></VALUE5>"
				+"<VALUE6>1</VALUE6>"//感官：固定 1
				+ "</inv:P_REQUEST_LINE_ITEM>");
		return sbStr.toString();
	}

	public static String getOrganizationId() {
		return organizationId;
	}

	public void setOrganizationId(String organizationId) {
		OracleEBSUtil.organizationId = organizationId;
	}

	public static String getGanGuanCode() {
		return ganGuanCode;
	}

	public void setGanGuanCode(String ganGuanCode) {
		OracleEBSUtil.ganGuanCode = ganGuanCode;
	}

	public static String getIfaceCode() {
		return ifaceCode;
	}

	public void setIfaceCode(String ifaceCode) {
		OracleEBSUtil.ifaceCode = ifaceCode;
	}

	public static String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		OracleEBSUtil.address = address;
	}
}
